﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Zyg.SSO.Core.Business;
using Zyg.SSO.Core;
using Zyg.Security;
using Zyg.Utility.Helper;

namespace Zyg.SSO.Web
{
    public partial class PassLogin : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null)
            {
                throw new Exception("非法请求");
            }

            if (Request.QueryString["APID"] == null)
            {
                throw new Exception("非法请求");
            }

            string strid = Request.QueryString["id"].ToString();
            string strAPID = Request.QueryString["APID"].ToString();
            AccountAppUser apuser = AccountAppUser.Load(new Guid(strid));
            APUserInfo userinfo = new APUserInfo();

            userinfo.PassWord = apuser.Password;
            userinfo.UserName = apuser.Name;
            string strUserinfo = APUserInfo.Serialize(userinfo);

            SecurityHelper shelper = new SecurityHelper();
            strUserinfo = shelper.Encrypt(strUserinfo, EncryptionAlgorithm.Des);
            strUserinfo = Server.UrlEncode(strUserinfo);

            string webServiceUrl = Zyg.SSO.Configuration.ConfigurationManager.Services.AuthenticationServices["AuthenticationService"].Url;
            string webServiceNamespace = Zyg.SSO.Configuration.ConfigurationManager.Services.AuthenticationServices["AuthenticationService"].Namespace;
            string webServiceClassName = Zyg.SSO.Configuration.ConfigurationManager.Services.AuthenticationServices["AuthenticationService"].ClassName;

            string strTicket = Zyg.Utility.WebServiceUtility.TryWebService(webServiceUrl, webServiceNamespace, webServiceClassName, "GetTicket", base.Account.Id.ToString(), Request.UserHostAddress, strAPID) as string;

            strTicket = Server.UrlEncode(strTicket);

            App app = App.Load(new Guid(strAPID));

            Response.Redirect(app.ToString() + "?Userinfo=" + strUserinfo + "&Ticket=" + strTicket);

        }
    }
}
